<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com>
// +----------------------------------------------------------------------

namespace Common\Model;
use Think\Model;
/**
 * 配置模型
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */

class AttachmentModel extends Model {
    protected $_validate = array(
        array('title', 'require', '标题不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('pdf_url', 'require', '请上传PDF文件', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('pdf_id', 'require', '请上传PDF文件', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('img_url', 'require', '请上传封面', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('img_id', 'require', '请上传封面', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('category', 'require', '分类不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('tags', 'require', '标签不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('description', 'require', '描述不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
    );

    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', '1', self::MODEL_BOTH),
    );
	
	/**
     * 获取配置列表
     * @return array 配置数组
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function addPdf($arr){
    	$data = array(
    		"pdf_url" => $arr['pdf_url'],
    		"pdf_id" => $arr['pdf_id'],
    		"img_url" => $arr['img_url'],
    		"img_id" => $arr['img_id'],
			"title" => $arr['title'],
			"category" => $arr['category'],
			"tags" => $arr['tags'],
			"description" => $arr['description'],
			"uid" => session('user_auth.uid'),
    		);
    	if($this->create($data)){
    		$aid = $this->add(); 
    		if($aid > 0){
    			//添加成功后处理其他事物
    			//M('AuthGroupAccess')->add(array('group_id'=> '2','uid' => $uid));
    			//处理标签的问题
    			$tags = explode( ',',$data['tags']);
    			$tagsId = array();
    			foreach ($tags as $key => $value) {
    				# code...
    				$tl = M('Tags')->where(array('value'=>$value))->find();
    				if (empty($tl)) {
    					$tie = M('Tags')->add(array('value'=>$value,'create_time'=>NOW_TIME));
    					$tagsId[] = $tie;
    				}else{
    					$tagsId[] = $tl['tid'];
    				}
    			}
    			$tagsArry = implode  ( ','  , $tagsId  );
 
    		}
    	}else{
    		$aid = -1;
    	}
    	return $aid;
    }

    /**
     * 获取配置列表
     * @return array 配置数组
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function lists(){
        $map    = array('status' => 1);
        $data   = $this->where($map)->field('type,name,value')->select();
        
        $config = array();
        if($data && is_array($data)){
            foreach ($data as $value) {
                $config[$value['name']] = $this->parse($value['type'], $value['value']);
            }
        }
        return $config;
    }

    /**
     * 根据配置类型解析配置
     * @param  integer $type  配置类型
     * @param  string  $value 配置值
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    private function parse($type, $value){
        switch ($type) {
            case 3: //解析数组
                $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
                if(strpos($value,':')){
                    $value  = array();
                    foreach ($array as $val) {
                        list($k, $v) = explode(':', $val);
                        $value[$k]   = $v;
                    }
                }else{
                    $value =    $array;
                }
                break;
        }
        return $value;
    }

}
